package com.journey.org.ui.splash

import android.Manifest
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.journey.org.BR
import com.journey.org.R
import com.journey.org.databinding.ActivitySplashBinding
import com.journey.org.ui.MainActivity
import com.mvvmhabit.org.base.BaseActivity
import com.mvvmhabit.org.utils.MaterialDialogUtils
import com.tbruyelle.rxpermissions2.RxPermissions

class SplashActivity : BaseActivity<ActivitySplashBinding, SplashViewModel>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 申请权限
        requestPermission()
    }

    override fun layoutId(savedInstanceState: Bundle?): Int = R.layout.activity_splash

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): Class<SplashViewModel> = SplashViewModel::class.java


    @SuppressLint("ObjectAnimatorBinding")
    override fun initData() {
        super.initData()
        // 加载本地 gif 动画
        // 加载本地 gif 动画
        Glide.with(this)
            .load(R.drawable.ic_splash_bg)
            .apply(RequestOptions().placeholder(R.drawable.ic_splash_bg))
            .into(mBinding.ivSplash)
    }

    // 放大缩小动画
    private fun startScaleAnimator() {
        val animator = AnimatorSet()
        val animatorYScale = ObjectAnimator.ofFloat(mBinding.ivSplash, "scaleY", 1f, 1.2f)
        val animatorXScale = ObjectAnimator.ofFloat(mBinding.ivSplash, "scaleX", 1f, 1.2f)
        animator.play(animatorYScale).with(animatorXScale)
        animator.duration = 3000
        animator.start()
    }

    // 创建线程
    private val thread = Thread(Runnable {
        mHandler.sendEmptyMessageDelayed(0, 1000)
    })

    private val mHandler: Handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            if (msg.what == 0) {
                removeHandlerMessage()
                if (mViewModel?.onTimeChangeField?.get() == 0) {
                    mViewModel?.onTimeChangeField?.set(0)
                    startActivity(MainActivity::class.java, null)
                    finish()
                } else {
                    removeMessageDelayed()
                    mViewModel?.onTimeChangeField?.set(mViewModel?.onTimeChangeField?.get()!! - 1)
                }
            }
        }
    }

    private fun removeHandlerMessage() {
        mHandler.removeMessages(0)
    }

    private fun removeMessageDelayed() {
        mHandler.sendEmptyMessageDelayed(0, 1000)
    }

    override fun onPause() {
        super.onPause()
        removeHandlerMessage()
    }

    @SuppressLint("CheckResult")
    private fun requestPermission() {
        val rxPermissions = RxPermissions(this)
        rxPermissions.request(
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.REQUEST_INSTALL_PACKAGES,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CALL_PHONE
        ).subscribe {
            if (!it) {
                thread.start()
                startScaleAnimator()
            } else {
                showPermissionsDialog()
            }
        }
    }

    private fun showPermissionsDialog() {
        MaterialDialogUtils.showBasicDialog(this, "系统提示！", "需要授权才能使用")
            .onPositive { _, _ ->
                run {
                    requestPermission()
                }
            }
            .onNegative { _, _ ->
                run {
                    if (!mViewModel?.isEnterMainField?.get()!!) {
                        startActivity(MainActivity::class.java, null)
                        finish()
                    }
                }
            }.show()
    }
}
